<template>
  <div>
    <el-table :data="tempData" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品名称">
              <span>{{ props.row.short_name }}</span>
            </el-form-item>
            <el-form-item label="商品 ID">
              <span>{{ props.row.goods_id }}</span>
            </el-form-item>
            <el-form-item label="商品分类">
              <span>{{ category[props.row.category - 1] }}</span>
            </el-form-item>
            <el-form-item label="商品价格">
              <span>{{ (props.row.price) | priceFormat }}</span>
            </el-form-item>
            <el-form-item label="商品库存">
              <span>{{ props.row.counts }}</span>
            </el-form-item>
            <el-form-item label="商品描述">
              <span>{{ props.row.goods_name }}</span>
            </el-form-item>
            <el-form-item label="商品图片">
              <img :src="props.row.thumb_url" style="width:70px" />
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="商品 ID" prop="goods_id">
      </el-table-column>
      <el-table-column label="商品名称" prop="short_name">
      </el-table-column>
      <el-table-column label="描述" prop="goods_name">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="props">
          <el-button size="mini" @click="handleEdit(props.$index, props.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(props.$index, props.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination background layout="prev, pager, next" :page-count="pageNum" @current-change="handleCurrentChange"
      :current-page="currentIndex">
    </el-pagination>
  </div>
</template>

<script>
import { getAllgoods, deleteRecomGoods } from './../../../api/index';
import { mapState } from 'vuex';
import { mapActions } from 'vuex'

export default {
  data() {
    return {
      category: ['美容美白', '养生保健', '医学书籍', '医疗器械', '视力保护', '运动器材', '清热解毒', '肠胃用药'],
      currentIndex: 1,
      pageSize: 5,
      tableData: [],
      tempData: [],
    }
  },
  mounted() {
    const page = this.$route.query.page;
    if (page) {
      this.currentIndex = Number(page);
    }
    this.getAllGoods();
  },
  methods: {
    handleEdit(index, row) {
      window.localStorage.setItem('goodsInfo', JSON.stringify(row));
      this.$router.replace({ path: '/admin/adminupdate', query: { page: this.currentIndex } });
    },
    async handleDelete(index, row) {
      this.$confirm('您确定永久删除该商品吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let result = await deleteRecomGoods(row.goods_id);
        if (result.success_code === 200) {
          this.$message({
            type: 'success',
            message: '已删除'
          });
          this.getAllGoods();
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleCurrentChange(val) {
      this.currentIndex = val;
      this.updateTempData();
    },
    updateTempData() {
      this.tempData = [];
      this.tableData.forEach((data, index) => {
        if (index >= (this.currentIndex - 1) * this.pageSize && index < this.currentIndex * this.pageSize) {
          this.tempData.push(data);
        }
      });
    },
    async getAllGoods() {
      let result = await getAllgoods();
      if (result.success_code === 200) {
        this.tableData = result.message;
        this.updateTempData();
      }
    },
  },
  computed: {
    pageNum() {
      return Math.ceil(this.tableData.length / this.pageSize);
    }
  },
  filters: {
    priceFormat(price) {
      return price.toFixed(2);
    },
  },
}
</script>

<style scoped>
.el-table {
  margin: 20px auto 50px;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.el-pagination {
  display: flex;
  justify-content: center;
  /* text-align: center; */
  margin-top: 20px;
}
</style>